__author__ = 'zhenjiao.su'
# -*- coding: utf-8 -*-
from excelrw import readxls
from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.action_chains import ActionChains
from Newwebdriver import Active
from time import sleep
import random
from Other import loading
class fhtools(Active):
    def __init__(self,driver):
        Active.__init__(self,driver)
        self.driver=driver
        self.exread=readxls("D:/pywebdriver/Pythonwebdriver/element1.xls")
        self.list_totalbutton=(self.exread(1,5),self.exread(1,6))  #查看详情按钮
        self.list_goflightname=(self.exread(3,5),self.exread(3,6))
        self.list_backflightname=(self.exread(4,5),self.exread(4,6))
        self.list_hotelname=(self.exread(5,5),self.exread(5,6))
        self.list_hoteltype=(self.exread(6,5),self.exread(6,6))
        self.list_sumprice=(self.exread(2,5),self.exread(2,6))
        self.list_flightinfo=("css selector",".list.js_t_jp_info")  #面板中的机票信息
        self.list_hotelinfo=("css selector",".h_room")  #面板中的酒店信息
        self.list_updateflight=(self.exread(25,5),self.exread(25,6))
        self.list_updatehotel=(self.exread(26,5),self.exread(26,6))
    def toolinfo(self):   #获取面板上的数据
        self.tgoflightname=self.find_element(self.list_goflightname).text
        self.tbackflightname=self.find_element(self.list_backflightname).text
        self.thotelname=self.find_element(self.list_hotelname).text
        self.thoteltype=self.find_element(self.list_hoteltype).text   #面板上的房型
        self.tsummoney=self.find_element(self.list_sumprice).text   #总价
    def listexit(self):   #判断是否进入list页，以是否有查看详情按钮
        try:
            WebDriverWait(self.find_element(self.list_totalbutton),5).until(lambda a:a)
            return True
        except:
            return False
    def tsubmit(self):   #查看详情提交
        self.find_element(self.list_totalbutton).click()
    def listupdate(self,updatetype):    #面板更换机票or更换酒店选择，参数机票=1，酒店=2
        tflight=self.find_element(self.list_flightinfo)   #面板中的机票信息
        thotel=self.find_element(self.list_hotelinfo)   #面板中的酒店信息
        if updatetype==1:
            updateflight=self.find_element(self.list_updateflight)
            ActionChains(self.driver).move_to_element(tflight).perform()       #将鼠标移动到机票面板上，判断是否是可以更换机票状态
            if updateflight.get_attribute("className")=="hover":
                updateflight.click()
        elif updatetype==2:
            updatehotel=self.find_element(self.list_updatehotel)
            ActionChains(self.driver).move_to_element(thotel).perform()
            if updatehotel.get_attribute("className")=="hover":
                updatehotel.click()
        else:
            print("参数错误")
            raise SystemError


class hotellist(fhtools):  #酒店list页
    def __init__(self,drive):
        fhtools.__init__(self,drive)
        self.list_hotel_loading=(self.exread(20,5),self.exread(20,6))
        self.list_hotel_resultfail=(self.exread(19,5),self.exread(19,6))   #搜索结果为空
        self.list_hotel_lists=(self.exread(12,5),self.exread(12,6))
        self.list_hotel_hotelroomtype=(self.exread(14,5),self.exread(14,6))
        self.list_hotel_chotype=(self.exread(16,5),self.exread(16,6))
        self.list_hotel_price=(self.exread(15,5),self.exread(15,6))
        self.list_hotel_hotelname=(self.exread(13,5),self.exread(13,6))
        self.list_hotel_typename=(self.exread(24,5),self.exread(24,6))
    def hotelloading(self):   #判断loading状态，loading消失，则false，超出30s一直loading则warning
        return loading(self.find_element(self.list_hotel_loading))
    def hotellistexit(self):   #判断list有没有数据，有则False，没有则True,
        try:
            WebDriverWait(self.driver,3).until(lambda a:a.find_element(*(self.list_hotel_resultfail)))
            return False
        except:
            return True
    def hotelselect(self):  #随机选择1则酒店，在随便选择该酒店下的某房间类型进行选择
        global shotel,shoteltype
        hotelnumber=self.find_elements(self.list_hotel_lists)
        shotel=random.randint(1,len(hotelnumber))
        hotelroomtype=self.find_elements(self.list_hotel_hotelroomtype,shotel)
        shoteltype=random.randint(2,len(hotelroomtype))
        hoteltypecleck=self.find_element(self.list_hotel_chotype,(shotel,shoteltype))
        if hoteltypecleck.get_attribute("className") !="ok":
            hoteltypecleck.click()
    def hotelcheckinfo(self):  #获取所选择的酒店名称，房间类型，与所选择的总价
        self.listhotelsumpay=self.find_element(self.list_hotel_price,(shotel,shoteltype)).text
        self.listhotelname=self.find_element(self.list_hotel_hotelname,shotel).text
        self.listhoteltypename=self.find_element(self.list_hotel_typename,(shotel,shoteltype)).text

class flightlist(fhtools):   #往返机票list页
    def __init__(self,drive):
        fhtools.__init__(self,drive)
        self.list_flight_goloading=(self.exread(17,5),self.exread(17,6))
        self.list_flight_endloading=(self.exread(18,5),self.exread(18,6))
        self.list_flight_gofail=(self.exread(21,5),self.exread(21,6))
        self.list_flight_endfail=(self.exread(22,5),self.exread(22,6))
        self.list_flight_golist=(self.exread(6,5),self.exread(6,6))
        self.list_flight_endlist=(self.exread(9,5),self.exread(9,6))
        self.list_flight_goname=(self.exread(8,5),self.exread(8,6))
        self.list_flight_gochoose=(self.exread(7,5),self.exread(7,6))
        self.list_flight_gooflight=(self.exread(27,5),self.exread(27,6))
        self.list_flight_endname=(self.exread(11,5),self.exread(11,6))
        self.list_flight_endchoose=(self.exread(10,5),self.exread(10,6))
        self.list_flight_endoflight=(self.exread(28,5),self.exread(28,6))
    def flightloading(self):  #判断是否是loading状态，不是loading状态，则True，否则False
        goloading=loading(self.find_element(self.list_flight_goloading))
        backloading=loading(self.find_element(self.list_flight_endloading))
        if goloading ==True and backloading==True:
            return True
        else:
            return False
    def flightlistexit(self):   #判断机票是否查询出数据，若有，则True，没有则false
        try:
            self.find_element(self.list_flight_gofail)
            self.find_element(self.list_flight_endfail)
            return False
        except:
            return True
    def flightlistinfo(self):  #获取机票list页，去和回的航空公司，需要判断是否是只有1个机票类型,以及机票选择
        #去程航空
        lengofightlist=len(self.find_elements(self.list_flight_golist))
        if lengofightlist>1:
            lengofightlists=random.randint(2,lengofightlist)
            self.goflighnamelist=self.find_element(self.list_flight_goname,lengofightlists).text
            self.find_element(self.list_flight_gochoose,lengofightlists).click()
        else:
            print(u"去程航空只有1个机票类型")
            self.goflighnamelist=self.find_element(self.list_flight_gooflight).text
        #回程航空
        sleep(5)
        lenbackflightlist=len(self.find_elements(self.list_flight_endlist))
        if lenbackflightlist>1:
            lenbackflightlists=random.randint(2,lenbackflightlist)
            self.backflighnamelist=self.find_element(self.list_flight_endname,lenbackflightlists).text
            self.find_element(self.list_flight_endchoose,lenbackflightlists).click()
        else:
            print(u"回程航空只有1个机票类型")
            self.backflighnamelist=self.find_element(self.list_flight_endoflight).text









